package com.ruoyi.web.controller.business;

import com.ruoyi.business.domain.TiffSeg;
import com.ruoyi.business.service.ITiffSegService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.sun.jna.platform.win32.Netapi32Util;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
@RequestMapping("/business/tiff")
public class TiffController extends BaseController {

    @Autowired
    ITiffSegService tiffSegService;

    /**
     * 获取用户列表
     */
    @PreAuthorize("@ss.hasPermi('business:tiff:list')")
    @GetMapping("/list")
    public TableDataInfo list(TiffSeg tiffSeg)
    {
        startPage();
        tiffSeg.setCreateBy(getUserId());
        List<TiffSeg> list = tiffSegService.selectTiffList(tiffSeg);
        return getDataTable(list);
    }

    /**
     * 根据用户编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:tiff:query')")
    @GetMapping(value = {"/{id}" })
    public AjaxResult getInfo(@PathVariable(value = "id") Long id)
    {
        AjaxResult ajax = AjaxResult.success();
        TiffSeg tiffSeg = tiffSegService.selectTiffById(id);
        if (tiffSeg == null)
        {
            ajax = AjaxResult.error("没有找到Tiff文件的详细信息！");
            return ajax;
        }
        ajax.put(AjaxResult.DATA_TAG, tiffSeg);
        return ajax;
    }

    /**
     * 新增用户
     */
    @PreAuthorize("@ss.hasPermi('business:tiff:add')")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody TiffSeg tiffSeg)
    {
        tiffSeg.setCreateBy(getUserId());
        tiffSeg.setUpdateBy(getUserId());
        return toAjax(tiffSegService.insertTiff(tiffSeg));
    }

    /**
     * 修改用户
     */
    @PreAuthorize("@ss.hasPermi('business:tiff:edit')")
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody TiffSeg tiffSeg)
    {
        tiffSeg.setUpdateBy(getUserId());
        return toAjax(tiffSegService.updateTiff(tiffSeg));
    }

    /**
     * 删除用户
     */
    @PreAuthorize("@ss.hasPermi('business:tiff:remove')")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tiffSegService.deleteTiffByIds(Arrays.asList(ids)));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('business:tiff:edit')")
    @PostMapping("/changeStatus/{id}/{status}")
    public AjaxResult changeStatus(@PathVariable Long id, @PathVariable String status)
    {
        return toAjax(tiffSegService.updateStatus(status, id));
    }

    /**
     * 上传文件
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile file) {
        AjaxResult ajax = tiffSegService.uploadFile(file);
        return ajax;
    }

    /**
     * 通过OSS下载Tiff文件
     * @param id
     * @return
     */
    @GetMapping("/downloadTiff/{id}")
    public AjaxResult downloadTiff(@PathVariable Long id) {
        AjaxResult ajax = tiffSegService.downloadTiff(id);
        return ajax;
    }

    /**
     * 生成分享链接和密码
     * @param id
     * @return
     */
    @PostMapping("/createShareUrl/{id}")
    public AjaxResult createShareUrl(@PathVariable Long id) {
        Long userId = getUserId();
        AjaxResult ajax = tiffSegService.createShareUrl(id, userId);
        return ajax;
    }

    /**
     * 获取预览地址
     * @param id
     * @return
     */
    @GetMapping("/previewTiff/{id}")
    public AjaxResult previewTiff(@PathVariable Long id) {
        AjaxResult ajax = tiffSegService.previewTiff(id);
        return ajax;
    }

    /**
     * 验证密码
     * @param password
     * @return
     */
    @PostMapping("/validPassword/{password}")
    public AjaxResult validPassword(@PathVariable String password) {
        AjaxResult ajax = tiffSegService.validPassword(password);
        return ajax;
    }

    /**
     * 开始分割
     * @param id
     * @return
     */
    @PostMapping("/segment/{id}")
    public AjaxResult segment(@PathVariable Long id) {
        AjaxResult ajax = tiffSegService.beginSeg(id);
        return ajax;
    }
}
